package main

import (
	"fmt"
	"jvmgo/classfile"
	"jvmgo/classpath"
)

func main() {
	cmd := parseCmd()
	//cmd.cpOption = "/Users/liyuan/IdeaProjects/handwriting-jvm/src"
	//cmd.class = "helloworld"
	//cmd.XjreOption = "/Library/Java/JavaVirtualMachines/jdk1.8.0_361.jdk/Contents/Home/jre"
	if cmd.versionFlag {
		fmt.Println("version liyuan 0.0.1")
	} else if cmd.helpFlag || cmd.class == "" {
		printUsage()
	} else {
		newJVM(cmd).start()
	}
}

//func startJVM(cmd *Cmd) {
//	cp := classpath.Parse(cmd.XjreOption, cmd.cpOption)
//	classLoader := heap.NewClassLoader(cp, cmd.verboseClassFlag)
//	className := strings.Replace(cmd.class, ".", "/", -1)
//	mainClass := classLoader.LoadClass(className)
//	mainMethod := mainClass.GetMainMethod()
//	if mainMethod != nil {
//		interpreter(mainMethod, cmd.verboseInstFlag, cmd.args)
//	} else {
//		fmt.Printf("Main Method not found in class %s\n", cmd.class)
//	}
//}

func getMainMethod(cf *classfile.ClassFile) *classfile.MemberInfo {
	for _, m := range cf.Methods() {
		if m.Name() == "main" && m.Descriptor() == "([Ljava/lang/String;)V" {
			return m
		}
	}
	return nil

}
func loadClass(className string, cp *classpath.Classpath) *classfile.ClassFile {
	clasData, _, err := cp.ReadClass(className)
	if err != nil {
		panic(err)
	}
	cf, err := classfile.Parse(clasData)
	if err != nil {
		panic(err)
	}
	return cf
}
